from django.contrib import admin
from .models import (
    EvaluationAnalysis, 
    ImprovementSuggestion, 
    TrainingRecommendation, 
    DriverImprovementMemory
)

@admin.register(EvaluationAnalysis)
class EvaluationAnalysisAdmin(admin.ModelAdmin):
    """评价分析管理"""
    list_display = ['id', 'order_rating', 'sentiment_label', 'sentiment_score', 'analysis_time']
    list_filter = ['sentiment_label', 'analysis_time']
    search_fields = ['order_rating__comment']
    readonly_fields = ['analysis_time']
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('order_rating')

@admin.register(ImprovementSuggestion)
class ImprovementSuggestionAdmin(admin.ModelAdmin):
    """改进建议管理"""
    list_display = ['id', 'analysis', 'suggestion_type', 'priority', 'generated_time']
    list_filter = ['suggestion_type', 'priority', 'generated_time']
    search_fields = ['suggestion_content']
    readonly_fields = ['generated_time']
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('analysis')

@admin.register(TrainingRecommendation)
class TrainingRecommendationAdmin(admin.ModelAdmin):
    """培训建议管理"""
    list_display = ['id', 'driver_id', 'recommendation_type', 'priority', 'status', 'created_time']
    list_filter = ['recommendation_type', 'priority', 'status', 'created_time']
    search_fields = ['recommendation_content']
    readonly_fields = ['created_time']
    
    actions = ['mark_as_pushed', 'mark_as_viewed', 'mark_as_completed']
    
    def mark_as_pushed(self, request, queryset):
        """标记为已推送"""
        updated = queryset.update(status=1)
        self.message_user(request, f'成功标记 {updated} 个培训建议为已推送')
    mark_as_pushed.short_description = "标记为已推送"
    
    def mark_as_viewed(self, request, queryset):
        """标记为已查看"""
        updated = queryset.update(status=2)
        self.message_user(request, f'成功标记 {updated} 个培训建议为已查看')
    mark_as_viewed.short_description = "标记为已查看"
    
    def mark_as_completed(self, request, queryset):
        """标记为已完成"""
        updated = queryset.update(status=3)
        self.message_user(request, f'成功标记 {updated} 个培训建议为已完成')
    mark_as_completed.short_description = "标记为已完成"

@admin.register(DriverImprovementMemory)
class DriverImprovementMemoryAdmin(admin.ModelAdmin):
    """司机改进记忆管理"""
    list_display = ['id', 'driver_id', 'memory_type', 'memory_key', 'importance_score', 'last_updated']
    list_filter = ['memory_type', 'importance_score', 'last_updated']
    search_fields = ['driver_id', 'memory_key']
    readonly_fields = ['created_time', 'last_updated']
    
    def get_queryset(self, request):
        return super().get_queryset(request).order_by('-last_updated')
